import request from '@/utils/request'
import * as qs from 'qs'

export function getList(query) {
  return request.get('/api/sys/company?' + qs.stringify(query))
    .then(json => {
      if (json.result) {
        return json.data
      } else {
        throw new Error(json.message)
      }
    })
}

export function count(query) {
  return request.get('/api/sys/company/count?' + qs.stringify(query))
    .then(json => {
      if (json.result) {
        return json.data
      } else {
        throw new Error(json.message)
      }
    })
}

export function approve(companyId, approved, message) {
  return request
    .post('/api/sys/company/approval',
      qs.stringify({ companyId, approved, description: message }),
      { headers: { 'content-type': 'application/x-www-form-urlencoded' }}
    )
    .then(json => {
      if (json.result) {
        return json.data
      } else {
        throw new Error(json.message)
      }
    })
}

export function getApprovalList(companyId, pageNo = 1, pageSize = 20) {
  return request.get(`/api/sys/company/${companyId}/approval?` + qs.stringify({ pageNo, pageSize }))
    .then(json => {
      if (json.result) {
        return json.data
      } else {
        throw new Error(json.message)
      }
    })
}

export function getApprovalCount(companyId) {
  return request.get(`/api/sys/company/${companyId}/approval`)
    .then(json => {
      if (json.result) {
        return json.data
      } else {
        throw new Error(json.message)
      }
    })
}

export function recommend(companyId) {
  return request.post(`/api/sys/company/recommend/${companyId}`)
    .then(json => {
      if (json.result) {
        return json.data
      } else {
        throw new Error(json.message)
      }
    })
}

export function cancelRecomment(companyId) {
  return request.post(`/api/sys/company/cancelRecomment/${companyId}`)
    .then(json => {
      if (json.result) {
        return json.data
      } else {
        throw new Error(json.message)
      }
    })
}
